﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace YJKCodeBase.WPF.ControlUtility
{
	public partial class ComboList : UserControl, IComponentConnector
	{
		public event ComboList.MyControlEventHandler OnComboboxSelectionChanged;

		public ComboList()
		{
			this.InitializeComponent();
		}

		public void Add(string type, string specification, Bitmap bmp)
		{
			int num = this.IndexOfTypeName(type);
			if (-1 != num)
			{
				Catalog catalog = new Catalog();
				catalog.CatalogName = specification;
				catalog.BmpFile = null;
				catalog.BmpHeight = 28;
				catalog.BmpWidth = 0;
				catalog.FontSize = 13;
				catalog.FontWeight = "Normal";
				catalog.IsType = false;
				this.m_lsCatalog.Insert(num, catalog);
			}
			else
			{
				BitmapSource bmpFile = this.BitmapToBitmapSource(bmp);
				Catalog catalog2 = new Catalog();
				catalog2.CatalogName = type;
				catalog2.BmpFile = bmpFile;
				catalog2.BmpHeight = 36;
				catalog2.BmpWidth = 36;
				catalog2.FontSize = 14;
				catalog2.FontWeight = "Bold";
				catalog2.IsType = true;
				this.m_lsCatalog.Add(catalog2);
				Catalog catalog3 = new Catalog();
				catalog3.CatalogName = specification;
				catalog3.BmpFile = null;
				catalog3.BmpHeight = 28;
				catalog3.BmpWidth = 0;
				catalog3.FontSize = 13;
				catalog3.FontWeight = "Normal";
				catalog3.IsType = false;
				this.m_lsCatalog.Add(catalog3);
			}
			this.cmbType.ItemsSource = this.m_lsCatalog;
		}

		public void SetSelectedIndex(int index)
		{
			this.cmbType.SelectedIndex = index;
		}

		public int GetSelectedIndex()
		{
			return this.cmbType.SelectedIndex;
		}

		public void SetCurrentText(string text)
		{
			foreach (Catalog catalog in this.m_lsCatalog)
			{
				if (catalog.CatalogName == text)
				{
					this.cmbType.SelectedItem = catalog;
					return;
				}
			}
			this.cmbType.SelectedIndex = 1;
		}

		public string GetCurrentText()
		{
			Catalog catalog = (Catalog)this.cmbType.SelectedItem;
			return catalog.CatalogName;
		}

		private int IndexOfTypeName(string typeName)
		{
			bool flag = false;
			int num = -1;
			foreach (Catalog catalog in this.m_lsCatalog)
			{
				num++;
				if (!flag && catalog.IsType && catalog.CatalogName == typeName)
				{
					flag = true;
				}
				else if (flag && catalog.IsType)
				{
					return num;
				}
			}
			if (flag)
			{
				return num + 1;
			}
			return -1;
		}

		private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			ComboBox comboBox = sender as ComboBox;
			Catalog catalog = comboBox.SelectedItem as Catalog;
			if (catalog.IsType)
			{
				comboBox.SelectedIndex++;
			}
			if (!catalog.IsType)
			{
				MyControlEventArgs args = new MyControlEventArgs(catalog.CatalogName);
				this.OnComboboxSelectionChanged(this, args);
			}
		}

		[DllImport("gdi32.dll")]
		public static extern bool DeleteObject(IntPtr hObject);

		private BitmapSource BitmapToBitmapSource(Bitmap bitmap)
		{
			IntPtr hbitmap = bitmap.GetHbitmap();
			BitmapSource result = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
			ComboList.DeleteObject(hbitmap);
			return result;
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		public void InitializeComponent()
		{
            //if (this._contentLoaded)
            //{
            //    return;
            //}
            //this._contentLoaded = true;
            //Uri resourceLocator = new Uri("/YJKCodeBase;component/wpf/combolist.xaml", UriKind.Relative);
            //Application.LoadComponent(this, resourceLocator);
		}

		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		[DebuggerNonUserCode]
		[EditorBrowsable(EditorBrowsableState.Never)]
		void IComponentConnector.Connect(int connectionId, object target)
		{
			if (connectionId == 1)
			{
				this.cmbType = (ComboBox)target;
				this.cmbType.SelectionChanged += this.ComboBox_SelectionChanged;
				return;
			}
			this._contentLoaded = true;
		}

		private List<Catalog> m_lsCatalog = new List<Catalog>();

		internal ComboBox cmbType;

		private bool _contentLoaded;

		public delegate void MyControlEventHandler(object sender, MyControlEventArgs args);
	}
}
